<template>
	<view class="img_cons">
		<view class="border_item"
			:style="{width:imageData.length==2||imageData.length==4?(imgWidth*2+10)+'px':(imgWidth*3+20)+'px'}">
			<template v-if="imageData.length==4">
				<view v-for="(item,index) in imageData" :key="item.id" class="split_img_4"
					:style="{width:imgWidth+'px',height:imgHeight+'px'}">
					<image :src="item.path" mode="" :style="{width:imgWidth+'px',height:imgHeight+'px'}" />
				</view>
			</template>
			<template v-else>
				<view v-for="(item,index) in imageData" :key="item.id" class="split_img"
					:style="{width:imgWidth+'px',height:imgHeight+'px'}">
					<image :src="item.path" mode="" :style="{width:imgWidth+'px',height:imgHeight+'px'}" />
				</view>
			</template>
			<!-- <image class="images_item" v-for="(item,index) in imgList" :key="item.id"  :src="item.url" :style="{width:item.width+'px',height:item.height+'px','margin-left':item.flex+'px','margin-right':item.flex+'px'}"></image> -->

		</view>

		<view class="save_btn" @click="saveAll">
			保存图片
		</view>
	</view>

</template>

<script>
	import {
		fileToBase64,
		scopeAlbum,
	} from '../../utils/common.js'

	export default {
		data() {
			return {
				imageData: [],
				imgWidth: 0,
				imgHeight: 0,

			}
		},
		onLoad(options) {
			let data = JSON.parse(options.data)
			console.log(data, '----')
			this.sortImg(data)
			this.imgWidth = options.width
			this.imgHeight = options.height

		},
		methods: {
			sortImg(data) {
				let dataImg = data
				dataImg.sort((a, b) => {
					return a.id > b.id ? 1 : -1;
				})
				this.imageData = dataImg
			},
			saveAll() {
				const that = this
				return scopeAlbum().then(function(response) {
					uni.showLoading({
						title: '正在保存...',
					})
					that.saveImage()
				})
			},
			saveImage() {
				let imgNum = this.imageData.length
				let count = 0
				for (let i = 0; i < this.imageData.length; i++) {
					uni.saveImageToPhotosAlbum({
						filePath: this.imageData[i].path,
						success() {
							count = count + 1
							uni.showLoading({
								title: `正在保存第${count}/${imgNum}张`,
							})
							if (count == imgNum) {
								uni.hideLoading()
							}
						},
						fail(e) {
							uni.hideLoading()
							uni.showModal({
								content: "保存相册失败，errCode：" + e.errCode + "，errMsg：" + e.errMsg +
									"，errSubject：" + e.errSubject,
								showCancel: false
							});
						}
					})
				}

			}
		}
	}
</script>

<style scoped>
	.img_cons {
		width: 100vw;
		min-height: 100vh;
		background-color: #0E0E12;
		padding-top: 0;
		padding-bottom: 200rpx;
		box-sizing: border-box;
		position: relative;
		overflow: hidden;
	}

	.border_item {
		width: 100%;
		margin: 0 auto;
		margin-top: 41rpx;
		display: flex;
		flex-wrap: wrap;
		align-self: center;
		justify-content: center;
	}

	.split_img {
		/* background-color: #B5B5DB; */
		margin-right: 10px;
		margin-bottom: 10px;
	}

	.split_img_4 {
		/* background-color: #B5B5DB; */
		margin-right: 10px;
		margin-bottom: 10px;
	}

	.border_item .split_img_4:last-child {
		margin-right: 0 !important;
	}

	.border_item .split_img_4:nth-child(2n) {
		margin-right: 0 !important;
	}

	.border_item .split_img:last-child {
		margin-right: 0 !important;
	}

	.border_item .split_img:nth-child(3n) {
		margin-right: 0 !important;
	}
	.save_btn{
	  width: 250rpx;
	  height: 78rpx;
	  margin: 0 auto;
	  margin-top: 40rpx;
	  background: #0E5AD3;
	  border-radius: 16rpx;
	  font-size: 26rpx;
	  font-family: PingFangSC-Medium, PingFang SC;
	  font-weight: bold;
	  color: #FFFFFF;
	  display: flex;
	  align-items: center;
	  justify-content: center;
	  line-height: 37rpx;
	}
</style>
